<?php
/**
 * Created by PhpStorm.
 * User: win10
 * Date: 2017/12/18
 * Time: 10:37
 */

namespace common\services;

require __DIR__ . '/qiniu/autoload.php';

use Yii;
use Qiniu\Storage\UploadManager;
use common\services\Qiniu;
use common\services\WXLogin\WeiXinLoginService;

class HttpUploadService{

    /**
     * 七牛上传图片调用方法
     * @param $filePath         // 文件名
     * @return bool|string      // 返回url
     */
    public static function qiNiuPictureUpload($filePath)
    {
        if(empty($filePath)){
            return false;
        }
        $objAuth = new Qiniu();
        $result  = $objAuth->uploadFile($filePath);
        if(empty($result) || !is_array($result)){
            return false;
        }
        $url = $objAuth->domain .'/'. $result['key'];
        return $url;
    }

    /**
     * base64位上传图片
     * @param $key
     * @param $filePath
     * @return bool
     */
    public static function qiNiuBase64PictureUpload($key,$filePath)
    {
        if(empty($filePath)){
            return false;
        }
        $objAuth = new Qiniu();
        $token = $objAuth->uploadToken($objAuth->bucket);
        $uploadMgr = new UploadManager();
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return false;
        } else {
            return 'http://' . $objAuth->domain .'/'. $ret['hash'];
        }
    }

    /**
     *
     */
    public static function WeiXinLogin($url)
    {
        $objWX = new WeiXinLoginService();
        $result = $objWX->get_authorize_url($url);
        return $result;
    }

}